<template>
  <div class="img-uploade-container">
    <input type="hidden" v-model="imageUrl">
    <img :src="imageUrl" class="uploaded-img" :style="previewStyle">
    <div class="img-select">
      <el-upload
        class="img-upload-box"
        :action="imgUploadApi"
        :show-file-list="false"
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
      >
        <el-button type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png/gif文件，且不超过500kb</div>
      </el-upload>
      <div class="img-upalod-group">
        <el-input v-model="netImgUrl" class="img-upalod-input" placeholder="网络图片链接"></el-input>
        <el-button type="primary" @click="openDialog">图片库</el-button>
      </div>
    </div>
    <el-dialog title="素材库" :visible.sync="showGallery" size="large">
      <gallery :openInDialog="true" :radio="true" @on-select="onSelectImg"></gallery>
    </el-dialog>
  </div>
</template>

<script>
import api from 'config/api'
import Gallery from '../views/Material/Gallery'

const placeholderImg = require('assets/Mobile/placeholder.png')

export default {
  name: 'ImgUpload',
  components: {
    Gallery,
  },
  props: {
    value: String,
    previewStyle: Object,
  },
  data() {
    return {
      imgUploadApi: api.getImgUploadApi(),
      imageUrl: this.value || placeholderImg,
      netImgUrl: '',
      showGallery: false,
    }
  },
  watch: {
    netImgUrl(val) {
      this.imageUrl = val
      this.$emit('input', this.imageUrl)
    },
    value(val) {
      this.imageUrl = val
      this.$emit('input', this.imageUrl)
    },
  },
  methods: {
    openDialog() {
      this.showGallery = true
    },
    onSelectImg(src) {
      this.showGallery = false
      this.imageUrl = src
      this.$emit('input', this.imageUrl)
    },
    handleSuccess(res) {
      this.imageUrl = res.data.showUlr
      this.$emit('input', this.imageUrl)
    },
    beforeUpload(file) {
      const isLt500K = file.size / 1024 / 1024 < 0.5

      if (!isLt500K) {
        this.$message.error('上传图片大小不能超过 500KB!')
      }
      return isLt500K
    },
  },
}
</script>
<style>
.img-uploade-container {
  min-width: 700px;
  margin-bottom: 20px;
}

.img-uploader {
  display: inline-block;
  width: 200px;
  height: 200px;
  vertical-align: top;
}

.img-select {
  display: inline-block;
  vertical-align: top;
  margin-left: 20px;
}

.img-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.img-uploader .el-upload:hover {
  border-color: #20a0ff;
}

.img-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.img-upload-box {
  margin-bottom: 12px;
}

.uploaded-img {
  width: 300px;
  min-height: 1px;
  display: inline-block;
  background-color: #ccc;
}

.img-upalod-group {
  display: inline-block;
  vertical-align: top;
  min-width: 300px;
}

.img-upalod-input {
  margin-bottom: 12px;
}
</style>
